package com.woniu.humanresource.controller;

import com.woniu.humanresource.entity.OrgStructure;
import com.woniu.humanresource.entity.PageBean;
import com.woniu.humanresource.entity.ResponseText;
import com.woniu.humanresource.exception.GeneralException;
import com.woniu.humanresource.service.OrgStructureService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class OrgStructureController {

    @Resource
    OrgStructureService orgStructureService;

    // 删除组织结构

    //提交删除申请
    @DeleteMapping("deleteApplyByOrgid/{orgid}")
    public ResponseText deleteJobRelationByPosition(@PathVariable("orgid") String orgid) {
        ResponseText responseText = null;
        try {
            orgStructureService.deleteApplyByOrgid(orgid);
            responseText = new ResponseText(null, 200, "提交删除组织结构申请成功");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }

    //获取组织结构具体信息
    @GetMapping("getOrgStructureByOrgid/{orgid}")
    public ResponseText selectOrgStructureByOrgid(@PathVariable("orgid") String orgid) {
        ResponseText<List<OrgStructure>> responseText = null;
        try {
            List<OrgStructure> orgStructureByOrgid = orgStructureService.selectOrgStructureByOrgid(orgid);
            responseText = new ResponseText<>(orgStructureByOrgid,200,"查询到职位关系的具体信息！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }


    // 添加组织结构
    @PostMapping("insertOrgStructure")
    public ResponseText insertOrgStructure(@RequestBody OrgStructure orgStructure) {
        ResponseText responseText = null;
        try {
            orgStructure.setOrgstatus(0);
            orgStructureService.insertOrgStructure(orgStructure);
            responseText = new ResponseText(null, 200, "添加组织结构成功！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }


    // 修改组织结构
    @PutMapping("updateOrg/{orgname}")
    public ResponseText updateOrg(@PathVariable("orgname") String orgname,
                                  @RequestBody OrgStructure orgStructure) {
        ResponseText responseText = null;
        try {
            orgStructureService.updateOrgStructure(orgStructure, orgname);
            responseText = new ResponseText(null, 200, "添加组织结构成功！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }


    // 查询所有组织结构
    @GetMapping("getAllOrgStructure/{pageIndex}/{pageSize}")
    public ResponseText selectAllOrgStructure(@PathVariable("pageIndex") int pageIndex,
                                       @PathVariable("pageSize") int pageSize
                                       ){
        ResponseText<PageBean<OrgStructure>> responseText = null;
        try {
            PageBean<OrgStructure> allOrgStructurepageBean = orgStructureService.getAllOrgStructure(pageIndex,pageSize);
            responseText = new ResponseText<>(allOrgStructurepageBean,200,"查询到所有职位关系！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
    //条件查询
    @PostMapping("getOrgStructure/{pageIndex}/{pageSize}")
    public ResponseText getOrgStructure(@PathVariable("pageIndex") int pageIndex,
                                        @PathVariable("pageSize") int pageSize,
                                        @RequestBody OrgStructure orgStructure) {
        ResponseText responseText = null;
        try {
            PageBean<OrgStructure> orgStructurepageBean = orgStructureService.getOrgStructure(orgStructure,pageIndex,pageSize);
            responseText = new ResponseText<>(orgStructurepageBean,200,"条件查询职位关系！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
}
